#include "MGUI_ImageButton.h"
#include "MGUI_Engine.h"

namespace Myway {

	ImplementRTTI(MGUI_ImageButton, MGUI_Widget);

	MGUI_ImageButton::MGUI_ImageButton(const MGUI_LookFeel * _lookfeel, MGUI_Widget * _parent)
		: MGUI_Widget(_lookfeel, _parent)
	{
		mTextBox = new MGUI_TextBox(this);
		mTextBox->SetAlign(MGUI_Align::Center);
	}

	MGUI_ImageButton::~MGUI_ImageButton()
	{
	}

	void MGUI_ImageButton::SetCaption(const MGUI_String & caption)
	{
		mTextBox->SetCaption(caption);
	}

	const MGUI_String & MGUI_ImageButton::GetCaption()
	{
		return mTextBox->GetCaption();
	}

	void MGUI_ImageButton::SetFont(MGUI_Font * font)
	{
		mTextBox->SetFont(font);
	}

	MGUI_Font * MGUI_ImageButton::GetFont()
	{
		return mTextBox->GetFont();
	}

	void MGUI_ImageButton::SetSkin(int _state, TexturePtr _skin, const MGUI_RectF & _rect)
	{
		mSkin[_state] = _skin;
		mUVRect[_state] = _rect;
	}

	void MGUI_ImageButton::SetSkin(int _state, const TString128 & _skin)
	{
		mSkin[_state] = TextureManager::Instance()->LoadTexture(_skin, _skin);
	}

	void MGUI_ImageButton::SetUVRect(int _state, const MGUI_RectF & _rect)
	{
		mUVRect[_state] = _rect;
	}

	void MGUI_ImageButton::UpdateRenderItem(MGUI_Layout * _layout)
	{
		if (!mVisible)
			return ;

		if (mSkin[mState] == NULL)
			return ;

		const MGUI_Rect & clipRect = GetParentClipRect();

		MGUI_Rect _rect; MGUI_RectF _uv;

		if (MGUI_Helper::Instance()->Clip(_rect, _uv, GetAbsRect(), mUVRect[mState], clipRect))
		{
			MGUI_RenderItem * ri = _layout->GetRenderItem(GetAbsOrder(), mSkin[mState].c_ptr());

			ri->AddQuad(MGUI_Helper::Instance()->GetQuad(_rect, _uv,  mColor));
		}

		for (int i = 0; i < mChildren.Size(); ++i)
		{
			mChildren[i]->UpdateRenderItem(_layout);
		}
	}


}